package com.jie.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.pojo.Tag;
import com.jie.service.TagsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/12/17:19
 * @Description:
 */
@Controller
@Slf4j
@RequestMapping("/admin")
public class TagsController {
    @Autowired
    private TagsService tagsService;

    @GetMapping("/tags")
    public String tag(@RequestParam(defaultValue = "1",value = "page") int page,
                      @RequestParam(defaultValue = "8",value = "pageSize") Integer pageSize,
                      Model model){
        //构造分页
        Page<Tag> pageInfo =new Page<>(page,pageSize);
        Page<Tag> pages = tagsService.page(pageInfo);
        model.addAttribute("page",pages);
        return "admin/tags";
    }
    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }
    @PostMapping("/tags")
    public String addTags(@Valid Tag tag, BindingResult result, RedirectAttributes redirectAttributes){
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName,tag.getName());
        Tag one = tagsService.getOne(queryWrapper);
        if(one!=null){
            result.rejectValue("name","nameError","不能添加重复的标签");
        }
        if(result.hasErrors()){
            return "admin/tags-input";
        }
        boolean save = tagsService.save(tag);
        if(save){
            redirectAttributes.addFlashAttribute("message","新增成功");
        }else {
            redirectAttributes.addFlashAttribute("message","新增失败");
        }
        return "redirect:/admin/tags";
    }
    @GetMapping("/tags/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("tag",tagsService.getById(id));
        return  "admin/tags-input";
    }
    @PostMapping("/tags/{id}")
    public String editTags(@Valid Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes redirectAttributes){
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName,tag.getName());
        Tag one = tagsService.getOne(queryWrapper);
        if(one!=null){
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        if(result.hasErrors()){
            return "admin/tags-input";
        }
        tag.setId(id);
        boolean save = tagsService.updateById(tag);
        if(save){
            redirectAttributes.addFlashAttribute("message","更新成功");
        }else {
            redirectAttributes.addFlashAttribute("message","更新失败");
        }
        return "redirect:/admin/tags";
    }
    @GetMapping("/tags/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
        tagsService.removeById(id);
        redirectAttributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/tags";
    }
}
